from django.db import transaction

from src.Business.audit.ReviewBase import ReviewProcessor
from common.models import TbOrganization, TbMiniappUser
from manager.serializer import ReviewSerializers


class OrganizationReview(ReviewProcessor):
    """
    师资认证审核类
    """

    def add_review(self, user: TbMiniappUser):
        organization = user.organization
        # 验证
        # --- start ---
        if user.role is not 20:
            raise ValueError("只有角色为机构的账号才能使用该功能")
        if organization.review_status is 2:
            raise ValueError("正在审核中")
        # if teacher.hasuploadedidcard is 0:
        #     raise ValueError("请上传身份证")
        # --- end ---
        with transaction.atomic():
            review = super().add_review(review_type=2, user=user)
            organization = user.organization
            organization.review_status = 2
            organization.review_id = review.uid
            organization.save()
            review.organization_id = organization.uid
            review.save()

    def get_review_list(self):
        review_list = super().get_review_list()
        review_list = review_list.filter(review_type=2)
        review_ser = ReviewSerializers(instance=review_list, many=True)
        return review_ser.data

    def process_reviews(self, **kwargs):
        """
        处理审核
        (0-未认证 1- 已认证 2-认证审核中 3- 认证审核被驳回
        """
        with transaction.atomic():
            review = super().process_reviews(**kwargs)
            organization = review.organization
            if review.review_status is 1:
                organization.review_status = 1
            else:
                organization.review_status = 3
            organization.save()
            return review
